import pytest
import json


# 定义待测接口
def add(x, y):
    return x + y


# data = [(10, 20, 30), (100, 200, 300), (1000, 2000, 3000)]
# 定义函数读取json文件，因为参数化的参2只支持[(),(),()]列表元组格式，而json文件是[{},{},{}],所以读取json文件转为参2列表元组格式
def read_json_data(filename):
    with open(filename, "r", encoding="utf8")as f:
        json_data = json.load(f)
        data_list = []
        for data in json_data:
            val = data.values()
            tmp = tuple(val)
            data_list.append(tmp)
        return data_list


class TestAdd(object):
    # 定义通用测试方法
    @pytest.mark.parametrize("x,y,expect", read_json_data("../add_data.json"))
    def test_add(self, x, y, expect):
        res = add(x, y)  # 调用待测接口，得到实际结果
        assert expect == res  # 断言


# def test01_add(self):
#     res = add(10, 20)
#     assert 30 == res
#
# def test02_add(self):
#     res = add(100,200)
#     assert 300 == res
#
# def test03_add(self):
#     res = add(1000,2000)
#     assert 3000 == res
if __name__ == '__main__':
    res = read_json_data("../add_data.json")
    print(res)
